﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKhachSan.BUS;
using QuanLyKhachSan.DTO;
using QuanLyKhachSan.DAO;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace QuanLyKhachSan
{
    public partial class frmLapPhieuThuePhong : Form
    {
        public frmLapPhieuThuePhong()
        {
            InitializeComponent();
        }
        //bien luu he thong hien tai
        private System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;

        private void LoadFrom()
        {
            DataTable ds = LoaiPhongBUS.LayDanhSachLoaiPhong();
            cboLoaiPhong.DataSource = ds;
            cboLoaiPhong.ValueMember = "tenLoaiPhong";
            string tenTK = cboLoaiPhong.Text;
            dgvDSPhongTrong.DataSource = PhongBUS.LayDanhSachPhongTheoLoaiCoHieuChinh(int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(tenTK)));
        }
        private void frmLapPhieuThuePhong_Load(object sender, EventArgs e)
        {
            //Lấy giờ hệ thống
            string gio = DateTime.Now.ToLongTimeString();
            string ngay = DateTime.Now.ToLongDateString();
            string chuoi = ngay + " " + gio;
            txtNgayHienTai.Text = chuoi;
            LoadFrom();
            btnHuy.Enabled = false;
            btnThuePhong.Enabled = false;

            //su dung he thong en-US
            //oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            
        }
        private void btnThemKH_Click(object sender, EventArgs e)
        {
            frmQuanLyKhachHang frm = new frmQuanLyKhachHang();
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            //bool themMoi = true;
            //frm.Tag = themMoi;
            frm.ShowDialog();

            //Lay DS KH moi them
            string tenKH = txtTenKhachHang.Text;
            DataTable dt = new DataTable();
            dt = KhachHangBUS.LayKhachHangMoiThem();
            dgvDSKH.DataSource = dt;
            
        }
        private void LoadKhiXong()
        {
            if (dgvDSKHChon.Rows.Count > 0)
            {
                for (int k = j-1; k >=0; k--)
                {
                    dgvDSKHChon.Rows.RemoveAt(k);
                }   
            }
            if (dgvDSPhongThue.Rows.Count > 0)
            {
                dgvDSPhongThue.Rows.RemoveAt(0);
            }
            j = 0;
            i = 0;
            txtTenKhachHang.Text = "";
            
            dgvDSPhongTrong.DataSource = null;
            btnThuePhong.Enabled = false;
            btnHuy.Enabled = false;
           
        }
        private void GhiFileEXCEL()
        {
            string tenPhong = dgvDSPhongThue.Rows[0].Cells["phongThue"].Value.ToString();
            DateTime ngayBatDauThue = DateTime.Now;
            // Khởi động chtr Excell
            COMExcel.Application exApp = new COMExcel.Application();

            exApp.Visible = false;
            // Thêm file temp xls
            COMExcel.Workbook exBook = exApp.Workbooks.Add(COMExcel.XlWBATemplate.xlWBATWorksheet);

            // Lấy sheet 1.
            COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1];

            COMExcel.Range r = (COMExcel.Range)exSheet.Cells[1, 1];
            COMExcel.Range r2 = (COMExcel.Range)exSheet.Cells[1, 5];

            // Ghi dữ liệu loi ngo
            r.Value2 = "PHIẾU THUÊ PHÒNG";
            r.Font.Bold = true;
            r.Font.Size = 16;
            r.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            r.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
            exApp.get_Range(r, r2).Merge(Type.Missing);
            // Giãn cột
            r.Columns.AutoFit();
            r.Rows.RowHeight = 30;

            COMExcel.Range rPhong = (COMExcel.Range)exSheet.Cells[2, 1];
            COMExcel.Range rPhong2 = (COMExcel.Range)exSheet.Cells[2, 2];
            rPhong.Value2 = "Phòng: " + tenPhong;
            rPhong.Font.Bold = true;
            rPhong.Font.Size = 13;
            exApp.get_Range(rPhong, rPhong2).Merge(Type.Missing);
            rPhong.Columns.AutoFit();

            COMExcel.Range rNgayThue = (COMExcel.Range)exSheet.Cells[2, 3];
            COMExcel.Range rNgayThue2 = (COMExcel.Range)exSheet.Cells[2, 5];
            rNgayThue.Value2 = "Ngày bắt đầu thuê: " + ngayBatDauThue.ToLongDateString();
            rNgayThue.Font.Bold = true;
            rNgayThue.Font.Size = 13;
            exApp.get_Range(rNgayThue, rNgayThue2).Merge(Type.Missing);
            rNgayThue.Columns.AutoFit();
            
            //dinh dang tieu de
            COMExcel.Range range = exApp.get_Range(exSheet.Cells[4, 1], exSheet.Cells[4, 5]);
            range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            //range.Value2 = "STT";
            range.Font.Bold = true;
            range.Font.Size = 12;
            range.Columns.ColumnWidth = 15;

            COMExcel.Range rSTT = (COMExcel.Range)exSheet.Cells[4, 1];
            rSTT.Value2 = "STT";

            COMExcel.Range rKH = (COMExcel.Range)exSheet.Cells[4, 2];
            rKH.Value2 = "Khách Hàng";

            COMExcel.Range rLoaiKhach = (COMExcel.Range)exSheet.Cells[4, 3];
            rLoaiKhach.Value2 = "Loại Khách";

            COMExcel.Range rCMND = (COMExcel.Range)exSheet.Cells[4, 4];
            rCMND.Value2 = "CMND";

            COMExcel.Range rDiaChi = (COMExcel.Range)exSheet.Cells[4, 5];
            rDiaChi.Value2 = "Địa Chỉ";
            for (int i = 0; i < dgvDSKHChon.Rows.Count; i++)
            {
                COMExcel.Range rDuLieuSTT = (COMExcel.Range)exSheet.Cells[i + 5, 1];
                rDuLieuSTT.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuSTT.Value2 = (i + 1);

                COMExcel.Range rDuLieuKH = (COMExcel.Range)exSheet.Cells[i + 5, 2];
                rDuLieuKH.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuKH.Value2 = dgvDSKHChon.Rows[i].Cells["khachHang"].Value.ToString();

                COMExcel.Range rDuLieuLoaiKH = (COMExcel.Range)exSheet.Cells[i + 5, 3];
                rDuLieuLoaiKH.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuLoaiKH.Value2 = dgvDSKHChon.Rows[i].Cells["loaiKhachHang"].Value.ToString();

                COMExcel.Range rDuLieuCMND = (COMExcel.Range)exSheet.Cells[i + 5, 4];
                rDuLieuCMND.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuCMND.Value2 = dgvDSKHChon.Rows[i].Cells["cmnd"].Value.ToString();

                COMExcel.Range rDuLieuDiaChi = (COMExcel.Range)exSheet.Cells[i + 5, 5];
                rDuLieuDiaChi.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                rDuLieuDiaChi.Value2 = dgvDSKHChon.Rows[i].Cells["diaChi"].Value.ToString();
            }
            exApp.Visible = true;
        }
        private void btnThuePhong_Click(object sender, EventArgs e)
        {
            if (dgvDSKHChon.Rows.Count == 0 || dgvDSPhongThue.Rows.Count == 0)
            {
                MessageBox.Show("Chua chon khach hang thue hoac phong thue", "Thong bao");
                return;
            }
            DialogResult result = MessageBox.Show("Ban chac chan muon thue phong khong?", "Thong Bao", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                PhieuThuePhongDTO phieuThuePhong = new PhieuThuePhongDTO();
                phieuThuePhong.DonGiaThue = float.Parse(dgvDSPhongThue.Rows[0].Cells["donGiaThue"].Value.ToString());
                phieuThuePhong.MaPhong = int.Parse(dgvDSPhongThue.Rows[0].Cells["maPhong"].Value.ToString());
                phieuThuePhong.NgayThue = DateTime.Parse(DateTime.Now.ToLongDateString());
                phieuThuePhong.PhuThuThue = float.Parse(txtPhuThu.Text.ToString());
                phieuThuePhong.HeSoThue = float.Parse(txtHeSo.Text.ToString());
                PhieuThuePhongBUS.PhieuThuePhongBUSResult ok = PhieuThuePhongBUS.Insert(phieuThuePhong);
                PhongBUS.ThuePhong(int.Parse(dgvDSPhongThue.Rows[0].Cells["maPhong"].Value.ToString()));
                int tongSoKhach = dgvDSKHChon.Rows.Count;
                for (int h = 0; h < tongSoKhach; h++)
                {
                    ChiTietPhieuThueDTO chiTietPhieuThue = new ChiTietPhieuThueDTO();
                    chiTietPhieuThue.MaPhieuThue = PhieuThuePhongBUS.LayMaPhieuThueMoiThem();
                    chiTietPhieuThue.MaKhachHang = int.Parse(dgvDSKHChon.Rows[h].Cells["maKhachHang"].Value.ToString());
                    ChiTietPhieuThueBUS.Insert(chiTietPhieuThue);
                }
                if (ok == PhieuThuePhongBUS.PhieuThuePhongBUSResult.OK)
                {
                    DialogResult dlr = MessageBox.Show("Lap Phieu Thue Phong Thanh Cong\nBan Co Muon Xuat Ra File EXCEL Khong?", "Thong Bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dlr == DialogResult.Yes)
                    {
                        GhiFileEXCEL();
                    }
                }
            }
            LoadKhiXong();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            dgvDSPhongThue.Rows.Clear();
            dgvDSKHChon.Rows.Clear();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
                
        private void txtTenKhachHang_TextChanged(object sender, EventArgs e)
        {
            string tenKH = txtTenKhachHang.Text;
            DataTable dt = new DataTable();
            dt = KhachHangBUS.LayDSKhachHangTheoTen(tenKH);
            dgvDSKH.DataSource = dt;
        }

        private void cboLoaiPhong_TextChanged(object sender, EventArgs e)
        {
            int index = cboLoaiPhong.SelectedIndex;
            if (index != -1)
            {
                string tenTK = cboLoaiPhong.Text;
                DataTable dt = new DataTable();
                dt = PhongBUS.LayDanhSachPhongTheoLoaiCoHieuChinh(int.Parse(LoaiPhongBUS.LayMaPhongTheoTen(tenTK)));
                
                dgvDSPhongTrong.DataSource = dt;
            }
        }
        int j = 0;
        private bool KiemTraKhachThue()
        {
            int vt = dgvDSKH.SelectedRows[0].Index;
            DataGridViewRow dr = dgvDSKH.Rows[vt];
            for (int r = 0; r < dgvDSKHChon.Rows.Count; r++)
            {
                if (dgvDSKHChon.Rows[r].Cells["maKhachHang"].Value.ToString() == dr.Cells[0].Value.ToString())
                {
                    MessageBox.Show("Khach da co trong muc phong muon thue", "Thong bao");
                    return false;
                }
            }
            return true;
        }
        private void btnChonKH_Click(object sender, EventArgs e)
        {
            if (dgvDSKH.SelectedRows.Count == 0)
            {
                MessageBox.Show("Vui long chon can thue can tra", "Thong bao");
                return;
            }
            else
            {
                btnHuy.Enabled = true;
                btnThuePhong.Enabled = true;
                int vt = dgvDSKH.SelectedRows[0].Index;
                DataGridViewRow dr = dgvDSKH.Rows[vt];
                if (dgvDSKHChon.Rows.Count == 0)
                {
                    dgvDSKHChon.Rows.Add();
                    dgvDSKHChon.Rows[j].Cells["sttKhach"].Value = j + 1;
                    dgvDSKHChon.Rows[j].Cells["maKhachHang"].Value = dr.Cells[0].Value.ToString();
                    dgvDSKHChon.Rows[j].Cells["khachHang"].Value = dr.Cells[1].Value.ToString();
                    dgvDSKHChon.Rows[j].Cells["loaiKhachHang"].Value = dr.Cells[2].Value.ToString();
                    dgvDSKHChon.Rows[j].Cells["cmnd"].Value = dr.Cells[3].Value.ToString();
                    dgvDSKHChon.Rows[j].Cells["diaChi"].Value = dr.Cells[4].Value.ToString();
                    j++;
                }
                else
                {
                    if (j >= QuyDinhBUS.SoKhachToiDa())
                    {
                        MessageBox.Show("Da dat toi so khach toi da cua mot phong", "Thong bao");
                        return;
                    }
                    if (KiemTraKhachThue() == false)
                    {
                        return;
                    }
                    else
                    {
                        dgvDSKHChon.Rows.Add();
                        dgvDSKHChon.Rows[j].Cells["sttKhach"].Value = j + 1;
                        dgvDSKHChon.Rows[j].Cells["maKhachHang"].Value = dr.Cells[0].Value.ToString();
                        dgvDSKHChon.Rows[j].Cells["khachHang"].Value = dr.Cells[1].Value.ToString();
                        dgvDSKHChon.Rows[j].Cells["loaiKhachHang"].Value = dr.Cells[2].Value.ToString();
                        dgvDSKHChon.Rows[j].Cells["cmnd"].Value = dr.Cells[3].Value.ToString();
                        dgvDSKHChon.Rows[j].Cells["diaChi"].Value = dr.Cells[4].Value.ToString();
                        j++;
                    }
                }
            }
            if (j == QuyDinhBUS.SoKhachToiDa())
            {
                float phuThu = QuyDinhBUS.PhuThu();
                txtPhuThu.Text = phuThu.ToString();
            }
            else
            {
                txtPhuThu.Text = "0";
            }
            float heSo = 1;
            for (int k = 0; k < j; k++)
            {
                float heSo1 = LoaiKhachHangBUS.LayHeSoTheoTenLoaiKH(dgvDSKHChon.Rows[k].Cells["loaiKhachHang"].Value.ToString());
                if (heSo<heSo1)//heSo != 1)
                {
                    heSo = heSo1;
                }
                
            }
            txtHeSo.Text = heSo.ToString();
        }
        int i = 0;
        private void btnChonPhong_Click(object sender, EventArgs e)
        {
            if (dgvDSPhongTrong.SelectedRows.Count == 0)
            {
                MessageBox.Show("Vui long chon phong can cho thue", "Thong bao");
                return;
            }
            else
            {
                if (i == 1)
                {
                    MessageBox.Show("Moi lan chi cho thue mot phong", "Thong bao");
                    return;
                }
                btnHuy.Enabled = true;
                btnThuePhong.Enabled = true;
                int vt = dgvDSPhongTrong.SelectedRows[0].Index;
                DataGridViewRow dr = dgvDSPhongTrong.Rows[vt];
                dgvDSPhongThue.Rows.Add();
                dgvDSPhongThue.Rows[i].Cells["sttThue"].Value = 1;
                dgvDSPhongThue.Rows[i].Cells["maPhong"].Value = dr.Cells[0].Value.ToString();
                dgvDSPhongThue.Rows[i].Cells["phongThue"].Value = dr.Cells[1].Value.ToString();
                dgvDSPhongThue.Rows[i].Cells["ghiChu"].Value = dr.Cells[2].Value.ToString();
                dgvDSPhongThue.Rows[i].Cells["donGiaThue"].Value = dr.Cells[3].Value.ToString();
                i++;
            }
        }

        private void btnHuyChonPhong_Click(object sender, EventArgs e)
        {
            if (dgvDSPhongThue.Rows.Count == 0)
            {
                return;
            }
            dgvDSPhongThue.Rows.Clear();
            i--;
        }

        private void btnHuyChonKH_Click(object sender, EventArgs e)
        {
            if (dgvDSKHChon.Rows.Count == 0)
            {
                return;
            }
            int vt = dgvDSKHChon.SelectedRows[0].Index;
            dgvDSKHChon.Rows.RemoveAt(vt);
            j--;
            if (j == QuyDinhBUS.SoKhachToiDa())
            {
                float phuThu = QuyDinhBUS.PhuThu();
                txtPhuThu.Text = phuThu.ToString();
            }
            else
            {
                txtPhuThu.Text = "0";
            }
            for (int k = 0; k < j; k++)
            {
                float heSo = LoaiKhachHangBUS.LayHeSoTheoTenLoaiKH(dgvDSKHChon.Rows[k].Cells["loaiKhachHang"].Value.ToString());
                if (heSo != 1)
                {
                    txtHeSo.Text = heSo.ToString();
                }
                else
                {
                    txtHeSo.Text = "1";
                }
            }
        }

        private void frmLapPhieuThuePhong_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
        }
    }
}